/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.command.executor;

import java.util.Collection;
import java.util.StringTokenizer;

import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.model.L1World;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1SkillUse;
import com.lineage.server.packetserver.S_SystemMessage;
import com.lineage.server.templates.L1Skills;
import com.lineage.server.utils.collections.Lists;

/**
 * GM指令：辅助魔法
 */
public class L1Buff implements L1CommandExecutor {
    public static L1CommandExecutor getInstance() {
        return new L1Buff();
    }

    private L1Buff() {
    }

    @Override
    public void execute(final L1PcInstance pc, final String cmdName,
            final String arg) {
        try {
            final StringTokenizer tok = new StringTokenizer(arg);
            Collection<L1PcInstance> players = null;
            String s = tok.nextToken();
            if (s.equalsIgnoreCase("me")) {
                players = Lists.newList();
                players.add(pc);
                s = tok.nextToken();
            } else if (s.equalsIgnoreCase("all")) {
                players = L1World.getInstance().getAllPlayers();
                s = tok.nextToken();
            } else {
                players = L1World.getInstance().getVisiblePlayer(pc);
            }

            final int skillId = Integer.parseInt(s);
            int time = 0;
            if (tok.hasMoreTokens()) {
                time = Integer.parseInt(tok.nextToken());
            }

            final L1Skills skill = SkillsTable.getInstance().getTemplate(
                    skillId);

            if (skill.getTarget().equals("buff")) {
                for (final L1PcInstance tg : players) {
                    new L1SkillUse().handleCommands(pc, skillId, tg.getId(),
                            tg.getX(), tg.getY(), null, time,
                            L1SkillUse.TYPE_SPELLSC);
                }
            } else if (skill.getTarget().equals("none")) {
                for (final L1PcInstance tg : players) {
                    new L1SkillUse().handleCommands(tg, skillId, tg.getId(),
                            tg.getX(), tg.getY(), null, time,
                            L1SkillUse.TYPE_GMBUFF);
                }
            } else {
                pc.sendPackets(new S_SystemMessage("非buff类型的魔法。"));
            }
        } catch (final Exception e) {
            pc.sendPackets(new S_SystemMessage("请输入 " + cmdName
                    + " [all|me] skillId time。"));
        }
    }
}
